package com.d3works.apps.businessproduction.service;

import com.d3works.apps.base.db.Product;
import com.d3works.apps.base.service.administration.SequenceService;
import com.d3works.apps.production.db.BillOfMaterial;
import com.d3works.apps.production.db.ProductionOrder;
import com.d3works.apps.production.db.repo.ProductionOrderRepository;
import com.d3works.apps.production.service.manuforder.ManufOrderService;
import com.d3works.apps.production.service.manuforder.ManufOrderService.ManufOrderOriginTypeProduction;
import com.d3works.apps.production.service.productionorder.ProductionOrderServiceImpl;
import com.d3works.apps.project.db.Project;
import com.d3works.apps.sale.db.SaleOrder;
import com.d3works.apps.sale.db.SaleOrderLine;
import com.d3works.exception.D3worksException;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;

public class ProductionOrderServiceBusinessImpl extends ProductionOrderServiceImpl {

  @Inject
  public ProductionOrderServiceBusinessImpl(
      ManufOrderService manufOrderService,
      SequenceService sequenceService,
      ProductionOrderRepository productionOrderRepo) {
    super(manufOrderService, sequenceService, productionOrderRepo);
  }

  @Transactional(rollbackOn = {Exception.class})
  public ProductionOrder generateProductionOrder(
      Product product,
      BillOfMaterial billOfMaterial,
      BigDecimal qtyRequested,
      Project project,
      LocalDateTime startDate,
      LocalDateTime endDate,
      SaleOrder saleOrder,
      SaleOrderLine saleOrderLine)
      throws D3worksException {

    ProductionOrder productionOrder = this.createProductionOrder(saleOrder);
    productionOrder.setProject(project);

    this.addManufOrder(
        productionOrder,
        product,
        billOfMaterial,
        qtyRequested,
        startDate,
        endDate,
        saleOrder,
        saleOrderLine,
        ManufOrderOriginTypeProduction.ORIGIN_TYPE_OTHER);

    return productionOrderRepo.save(productionOrder);
  }
}
